<template>
  <div>
    <div style="padding: 10px 0">
      <el-input style="width: 200px" placeholder="请输入帖子标题查询" suffix-icon="el-icon-search"
                v-model="ptitle"></el-input>
      <el-input style="width: 200px" placeholder="请输入帖子发布人查询" suffix-icon="el-icon-search" class="ml-5"
                v-model="uname"></el-input>
      <el-input style="width: 200px" placeholder="请输入帖子板块查询" suffix-icon="el-icon-search" class="ml-5"
                v-model="pplate"></el-input>

      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    </div>
    <div style="margin: 10px 0">
      <el-popconfirm
          class="ml-5"
          confirm-button-text='确定'
          cancel-button-text='我再想想'
          icon="el-icon-info"
          icon-color="red"
          title="你确定批量删除这些数据吗？"
          @confirm="delBatch"
      >
        <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>

      <el-dropdown split-button type="success" class="ml-5">
        按浏览量排序
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-model="plook" @click.native="handerLookAsc">升序</el-dropdown-item>
          <el-dropdown-item v-model="plook" @click.native="handerLookDesc">降序</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown split-button type="warning" class="ml-5">
        按点赞量排序
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-model="puporder" @click.native="handerUpAsc">升序</el-dropdown-item>
          <el-dropdown-item v-model="puporder" @click.native="handerUpDesc">降序</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown split-button type="primary" class="ml-5">
        按收藏量排序
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-model="plike" @click.native="handerLikeAsc">升序</el-dropdown-item>
          <el-dropdown-item v-model="plike" @click.native="handerLikeDesc">降序</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown split-button type="danger" class="ml-5">
        按被举报数排序
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-model="preport" @click.native="handerReportAsc">升序</el-dropdown-item>
          <el-dropdown-item v-model="preport" @click.native="handerReportDesc">降序</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-dropdown split-button type="success" class="ml-5">
        按帖子状态显示
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item v-model="pstatuss" @click.native="handerStatusN">正常</el-dropdown-item>
          <el-dropdown-item v-model="pstatuss" @click.native="handerStatusG">精华</el-dropdown-item>
          <el-dropdown-item v-model="pstatuss" @click.native="handerStatusU">禁用</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <el-table :data="tableData" border stripe
              :header-cell-class-name="'headerBg'"
              :header-cell-style="{'text-align':'center'}"
              :cell-style="{'text-align':'center'}" @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>

      <el-table-column prop="ptitle" label="帖子标题" width="180px">
      </el-table-column>
      <el-table-column prop="pplate" label="帖子板块" width="80px">
      </el-table-column>
      <el-table-column prop="ptime" label="发布时间" width="140px">
      </el-table-column>
      <el-table-column prop="preported" label="被举报数" width="80px">
      </el-table-column>
      <el-table-column prop="uname" label="发布者" width="120px">
      </el-table-column>
      <el-table-column prop="pup" label="点赞总数" width="80px">
      </el-table-column>
      <el-table-column prop="pconment" label="评论总数" width="80px">
      </el-table-column>
      <el-table-column prop="pview" label="浏览量" width="80px">
      </el-table-column>
      <el-table-column prop="plike" label="收藏量" width="80px">
      </el-table-column>
      <el-table-column label="处理操作">
        <template slot-scope="scope">
          <el-button type="success" @click="handerRemove(scope.row)">转移 <i class="el-icon-folder-remove"></i>
          </el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定置顶帖子吗？"
              @confirm="handerTop(scope.row.pid)"
          >
            <el-button type="primary" @click="handerTop(scope.row)">置顶 <i class="el-icon-upload2"></i></el-button>
          </el-popconfirm>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定精华帖子吗？"
              @confirm="handerGood(scope.row.pid)"
          >
            <el-button type="warning" slot="reference">精华 <i class="el-icon-star-on"></i></el-button>
          </el-popconfirm>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定删除吗？"
              @confirm="del(scope.row.pid)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>

          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="你确定禁用吗？"
              @confirm="handerunGood(scope.row.pid)"
          >
            <el-button type="warning" slot="reference">禁用 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
          <el-popconfirm
                  class="ml-5"
                  confirm-button-text='确定'
                  cancel-button-text='我再想想'
                  icon="el-icon-info"
                  icon-color="red"
                  title="你确定恢复该帖子状态吗？"
                  @confirm="LetPass(scope.row.pid)"
          >
            <el-button type="primary" slot="reference">恢复 <i class="el-icon-circle-check"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="板块转移" :visible.sync="dialogFormVisible" width="30%">
      <el-form label-width="100px" size="small">
        <el-form-item label="帖子发布者">
          <el-input readonly v-model="form.uname" aria-readonly="true" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-input readonly v-model="form.ptime" aria-readonly="true" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="帖子板块">
          <el-input v-model="form.pplate" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "Post",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,
      ptitle: "",
      uname: "",
      pplate: "",
      plook: "",
      puporder: "",
      plike: "",
      preport: "",
      pstatuss: "",
      form: {},
      readonly: true,
      dialogFormVisible: false,
      multipleSelection: [],
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      this.request.get("/post/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          uname: this.uname,
          ptitle: this.ptitle,
          pplate: this.pplate,
          plook: this.plook,
          puporder: this.puporder,
          plike: this.plike,
          preport: this.preport,
          pstatuss: this.pstatuss
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.records
        this.total = res.total
      })

    },
    tozero() {
      this.puporder = "",
          this.plook = "",
          this.preport = "",
          this.plike = ""
    },
    save() {
      this.request.post("/post/insert", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handerGood(id) {
      this.request.get("/post/good/" + id).then(res => {
        if (res) {
          this.$message.success("精华成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("已为精华帖子")
        }
      })
    },
    handerunGood(id) {
      this.request.get("/post/ungood/" + id).then(res => {
        if (res) {
          this.$message.success("禁用成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("已为禁用帖子")
        }
      })
    },
    handerRemove(row) {
      this.form = row
      this.dialogFormVisible = true
    },

    handerTop(id) {
      this.request.get("/post/top/" + id).then(res => {
        if (res) {
          this.$message.success("置顶成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("已为置顶帖子")
        }
      })
    },
    del(id) {
      this.request.delete("/post/delete/" + id).then(res => {
        if (res) {
          this.$message.success("删除成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      // console.log(val)
    },
    delBatch() {
      let pids = this.multipleSelection.map(v => v.pid)
      this.request.post("/post/del/batch", pids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handerLookAsc() {
      this.tozero()
      this.plook = 1
      this.load()
    },
    handerLookDesc() {
      this.tozero()
      this.plook = 0
      this.load()
    },
    handerUpAsc() {
      this.tozero()
      this.puporder = 0
      this.load()
    },
    handerUpDesc() {
      this.tozero()
      this.puporder = 1
      this.load()
    },
    handerLikeAsc() {
      this.tozero()
      this.plike = 0
      this.load()
    },
    handerLikeDesc() {
      this.tozero()
      this.plike = 1
      this.load()
    },
    handerReportAsc() {
      this.tozero()
      this.preport = 0
      this.load()
    },
    handerReportDesc() {
      this.tozero()
      this.preport = 1
      this.load()
    },
    handerStatusN() {
      this.pstatuss = 0
      this.load()
    },
    handerStatusG() {
      this.pstatuss = 2
      this.load()
    },
    handerStatusU() {
      this.pstatuss = 1
      this.load()
    },
    LetPass(id){
      this.request.get("/post/Nstatus/"+id).then(res=>{
        if(res){
          this.$message.success("操作成功")
          this.dialogFormVisible=false
          this.load()
        }else{
          this.$message.error("操作失败")
        }
      })
    },
  }
}
</script>
<style>
.headerBg {
  background-color: #eee !important;

}
</style>

<!--对日期排序查询-->